package cc.vv.baselibrary.db;

import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.support.ConnectionSource;

import cc.vv.baselibrary.db.daTable.ChatMsgDNDTable;
import cc.vv.baselibrary.db.daTable.ChatToTopTable;
import cc.vv.baselibrary.db.daTable.GroupMembersInfoObj;
import cc.vv.baselibrary.db.daTable.UserDetailInfoTable;
import cc.vv.lklibrary.db.help.DBBaseOpenHelper;
import cc.vv.lklibrary.db.operate.DBTableOperate;

/**
 * 公共数据库配置
 *
 * @author: NPF
 * @date: 2018/3/13.
 */
public class BaseDBHelper extends DBBaseOpenHelper {
    /**
     * 数据库名称
     */
    private static String DBNAME = "UTime_DB";
    /**
     * 数据库版本
     */
    private static final int DBVERSION = 4;

    public BaseDBHelper(String dbName, int dbVersion, Class... tableBeanClass) {
        super(dbName, dbVersion, tableBeanClass);
    }

    private static BaseDBHelper mInstance;

    /**
     * 配置表映射实体
     */
    public static BaseDBHelper getInstance() {
//        String mineAccount = UserInfoManageUtil.getUserId();
//        if (!TextUtils.isEmpty(mineAccount)) {
//            mineAccount = mineAccount.replace("-", "");
//        }
//        String currentDBName = String.format(Locale.CHINESE, DBNAME+"%s", mineAccount);
//        if (!currentDBName.equals(DBNAME)) {
//            mInstance = null;
//        }
        if (null == mInstance) {
            synchronized (BaseDBHelper.class) {
                if (null == mInstance) {
                    mInstance = new BaseDBHelper(DBNAME, DBVERSION, UserDetailInfoTable.class, ChatToTopTable.class, ChatMsgDNDTable.class
                            , GroupMembersInfoObj.class);
                }
            }
        }
        return mInstance;
    }

    /**
     * 数据库更新
     *
     * @param sqLiteDatabase
     * @param connectionSource
     * @param i
     * @param i1
     */
    @Override
    protected void update(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        // 清空原常用联系人表数据
        sqLiteDatabase.execSQL("delete from topContacts");
        // 更新表, 添加 mobile 字段
        DBTableOperate.getInstance().columnAdd(sqLiteDatabase, "topContacts", "mobile", "varchar", "10086");
    }
}